package com.ssps.back.platform.service.Impl;

import com.ssps.back.platform.service.OrdersService;
import com.ssps.entity.*;
import com.ssps.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author li
 * @time 2020/07/22 20:34:38
 */

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    ParkingMapper parkingMapper;
    @Autowired
    UserCouponMapper userCouponMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CommissionMapper commissionMapper;
    @Autowired
    AmountAccountMapper amountAccountMapper;
    /*
     *
     * 查询
     * 通过姓名反查id
     *
     */

    @Override
    public List<Orders> findAll(String stutas, String uname) {
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.createCriteria();
        if (stutas != "" && stutas != null) {
            criteria.andStatusEqualTo(stutas);
        }
        List<Integer> uiids = new ArrayList<>();
        if (uname != "" && uname != null) {
            UserInfoExample example1 = new UserInfoExample();
            UserInfoExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andUnameLike("%" + uname + "%");
            List<UserInfo> userInfoList = userInfoMapper.selectByExample(example1);
            if (userInfoList.size() > 0) {
                for (UserInfo userInfo : userInfoList) {
                    uiids.add(userInfo.getUiid());
                }
                criteria.andUiidIn(uiids);
            }
        }

        List<Orders> ordersList = ordersMapper.selectByExample(example);
        for (Orders orders : ordersList) {
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(orders.getUiid());
            Parking parking = parkingMapper.selectByPrimaryKey(orders.getPid());
            UserCoupon userCoupon = userCouponMapper.selectByPrimaryKey(orders.getUcid());
            if (userCoupon != null) {
                Coupon coupon = couponMapper.selectByPrimaryKey(userCoupon.getCpid());
                orders.setCoupon(coupon);
            }
            orders.setUserInfo(userInfo);
            orders.setParking(parking);
        }
        return ordersList;
    }

    @Override
    public void ordersUpdate(Orders orders) {
        ordersMapper.updateByPrimaryKeySelective(orders);
    }

    @Override
    public void ordersCents(Orders orders) {

        Orders newOrder = ordersMapper.selectByPrimaryKey(orders.getPid2());
        Long money = newOrder.getPrice();
        System.out.println(money);

        //优惠券金额
        Long couponMoney;
        if (newOrder.getUcid() != null) {
            UserCoupon userCoupon = userCouponMapper.selectByPrimaryKey(newOrder.getUcid());
            Coupon coupon = couponMapper.selectByPrimaryKey(userCoupon.getCpid());
            couponMoney = coupon.getMoney();
        } else {
            couponMoney = 0L;
        }

        //实际应分成的金额
        Long newMoney = money - couponMoney;
        Commission commission = commissionMapper.selectByPrimaryKey(newOrder.getCmid());

        //平台所得
        Long platformMoney = (newMoney * commission.getPlatform()) / 100;
        System.out.println(platformMoney);
        //物业所得
        Long propertyMoney = (newMoney * commission.getProperty()) / 100;
        System.out.println(propertyMoney);
        //用户所得
        Long userInfoMoney = (newMoney * commission.getRent()) / 100;
        System.out.println(userInfoMoney);

        //根据成为算车用户账户 给用户账户分钱
        ParkingExample example = new ParkingExample();
        ParkingExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(newOrder.getPid());
        List<Parking> parkingList = parkingMapper.selectByExample(example);
        Parking parking = new Parking();
        if (parkingList != null) {
            parking = parkingList.get(0);
        }
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(parking.getUiid());
        userInfo.setBalance(userInfo.getBalance().add(new BigDecimal(userInfoMoney)));
        userInfoMapper.updateByPrimaryKeySelective(userInfo);


        AmountAccount amountAccount = new AmountAccount();
        List<AmountAccount> amountAccountList = amountAccountMapper.selectByExample(null);
        if (amountAccountList != null) {
            amountAccount = amountAccountList.get(0);

            amountAccount.setAmountFrozen(amountAccount.getAmountFrozen().subtract(new BigDecimal(newMoney)));
            amountAccount.setPlatformAmount(amountAccount.getPlatformAmount().add(new BigDecimal(platformMoney)));
            amountAccount.setPropertyAmount(amountAccount.getPropertyAmount().add(new BigDecimal(propertyMoney)));
            amountAccountMapper.updateByPrimaryKeySelective(amountAccount);
        }
        ordersMapper.updateByPrimaryKeySelective(orders);

    }

    @Override
    public void ordersRefund(Orders orders) {

        Orders newOrder = ordersMapper.selectByPrimaryKey(orders.getPid2());
        //优惠券金额
        Long couponMoney;
        if (newOrder.getUcid() != null) {
            UserCoupon userCoupon = userCouponMapper.selectByPrimaryKey(newOrder.getUcid());
            Coupon coupon = couponMapper.selectByPrimaryKey(userCoupon.getCpid());
            couponMoney = coupon.getMoney();
        } else {
            couponMoney = 0L;
        }
        Long money = newOrder.getPrice();
        //实际应退还的金额
        Long newMoney = money - couponMoney;
        System.out.println(newMoney);

        Parking parking = parkingMapper.selectByPrimaryKey(newOrder.getPid());
        UserInfo userInfo = new UserInfo();
        if (parking != null) {
            UserInfoExample infoExample = new UserInfoExample();
            UserInfoExample.Criteria criteria = infoExample.createCriteria();
            criteria.andUiidEqualTo(parking.getUiid());
            List<UserInfo> userInfoList = userInfoMapper.selectByExample(infoExample);
            if (userInfoList != null) {
                userInfo = userInfoList.get(0);
                userInfo.setBalance(userInfo.getBalance().add(new BigDecimal(newMoney)));
                userInfoMapper.updateByPrimaryKeySelective(userInfo);
                List<AmountAccount> accountList = amountAccountMapper.selectByExample(null);
                AmountAccount amountAccount = accountList.get(0);
                amountAccount.setAmountFrozen(amountAccount.getAmountFrozen().subtract(new BigDecimal(newMoney)));
                amountAccountMapper.updateByPrimaryKeySelective(amountAccount);
                ordersMapper.updateByPrimaryKeySelective(orders);
            }
        }
    }

    @Override
    public Map<String, Double> findSaleData(String start, String end) {
        System.out.println(start);
        System.out.println(end);
        Map<String, Double> map = new HashMap();
        try {
            //根据用户名，开始时间，结束时间查询订单
            OrdersExample example = new OrdersExample();
            OrdersExample.Criteria criteria = example.createCriteria();
            //将String 转换成Date
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dateStart = dateFormat.parse(start);
            Date dateEnd = dateFormat.parse(end);
            //添加日期查询条件
            criteria.andStarttimeBetween(dateStart, dateEnd);
            //添加状态查询条件
//            criteria.andStatusEqualTo("已完成");
            List<Orders> orderList = ordersMapper.selectByExample(example);
            //确保有数据再去订单明细表里查询数量
            if (orderList != null && orderList.size() > 0) {
                //遍历订单集合，查询每个订单的销售数量
                for (Orders order : orderList) {
                    Date paymentTime = order.getStarttime();
                    //Date换成String
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String dateStr = sdf.format(paymentTime);
                    //获取支付金额
                    Long couponMoney;
                    if (order.getUcid() != null) {
                        UserCoupon userCoupon = userCouponMapper.selectByPrimaryKey(order.getUcid());
                        Coupon coupon = couponMapper.selectByPrimaryKey(userCoupon.getCpid());
                        couponMoney = coupon.getMoney();
                    } else {
                        couponMoney = 0L;
                    }
                    Long money = order.getPrice();
                    //实际应退还的金额
                    Long newMoney = money - couponMoney;
                    if(map.containsKey(dateStr)){
                        //累加销售额
                        map.put(dateStr,newMoney.doubleValue()+map.get(dateStr));
                    }else {
                        map.put(dateStr,newMoney.doubleValue());
                    }
                }
            } else {
                return new HashMap();
            }

        } catch (ParseException e) {
            e.printStackTrace();
            return new HashMap();
        }
        return map;
    }

}
